#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"stb/stb_image.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    BHXJNavTreeNode * n= this->ui->listView->addNode("BHXJControls2D");
    BHXJNavTreeNode * nn;
    nn=this->ui->listView->addNode("波浪进度sin",n);
    connect(nn,&BHXJNavTreeNode::clicked,this,&MainWindow::onWaveSin_clicked);

    nn=this->ui->listView->addNode("波浪进度shader",n);
    connect(nn,&BHXJNavTreeNode::clicked,this,&MainWindow::onWaveShader_clicked);

    nn=this->ui->listView->addNode("罗盘",n);
    connect(nn,&BHXJNavTreeNode::clicked,this,&MainWindow::onCompass_clicked);



    n= this->ui->listView->addNode("BHXJControls3D");
    nn=this->ui->listView->addNode("速度表&转速表",n);
    connect(nn,&BHXJNavTreeNode::clicked,this,&MainWindow::onSpeedoMeter_clicked);
    nn=this->ui->listView->addNode("ttf字体",n);
    connect(nn,&BHXJNavTreeNode::clicked,this,&MainWindow::onTTFTest_clicked);
    nn=this->ui->listView->addNode("规则体",n);
    connect(nn,&BHXJNavTreeNode::clicked,this,&MainWindow::onRegularShape_clicked);
    nn=this->ui->listView->addNode("水面模拟",n);
    connect(nn,&BHXJNavTreeNode::clicked,this,&MainWindow::onWaterWave_clicked);

    this->ui->listView->explandAll();
    new QVBoxLayout(this->ui->widgetPlay);
    //this->ui->widgetPlay->setLayout(lay);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::onWaveSin_clicked()
{
//    DialogWaveSin dialog(this);
//    dialog.setWindowFlags(dialog.windowFlags()|Qt::WindowMinMaxButtonsHint);
//    dialog.exec();

    DialogWaveSin *dialog=new DialogWaveSin(this);
    dialog->setAttribute(Qt::WA_DeleteOnClose,true);
    dialog->setWindowFlags(dialog->windowFlags()|Qt::WindowMinMaxButtonsHint);
    this->clearTestWidget();
    this->ui->widgetPlay->layout()->addWidget(dialog);
}

void MainWindow::onCompass_clicked()
{
//    DialogCompass dialog(this);
//    dialog.setWindowFlags(dialog.windowFlags()|Qt::WindowMinMaxButtonsHint);
//    dialog.exec();

    DialogCompass *dialog=new DialogCompass(this);
    dialog->setAttribute(Qt::WA_DeleteOnClose,true);
    dialog->setWindowFlags(dialog->windowFlags()|Qt::WindowMinMaxButtonsHint);
    this->clearTestWidget();
    this->ui->widgetPlay->layout()->addWidget(dialog);
}

void MainWindow::onWaveShader_clicked()
{
//    DialogWaveShader dialog(this);
//    dialog.setWindowFlags(dialog.windowFlags()|Qt::WindowMinMaxButtonsHint);
//    dialog.exec();

    DialogWaveShader *dialog=new DialogWaveShader(this);
    dialog->setAttribute(Qt::WA_DeleteOnClose,true);
    dialog->setWindowFlags(dialog->windowFlags()|Qt::WindowMinMaxButtonsHint);
    this->clearTestWidget();
    this->ui->widgetPlay->layout()->addWidget(dialog);
}

void MainWindow::onTTFTest_clicked()
{
    //    Formttftest *dialog=new Formttftest(this);
    //    dialog->setAttribute(Qt::WA_DeleteOnClose,true);
    //    //dialog->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    //   // dialog->setWindowFlags(Qt::WindowTitleHint | Qt::Dialog|Qt::WindowMinMaxButtonsHint|Qt::WindowCloseButtonHint);
    //    dialog->setWindowFlags(dialog->windowFlags()|Qt::Dialog);
    //    //dialog->setWindowModality(Qt::WindowModal);
    //    dialog->setAttribute(Qt::WA_ShowModal, true);
    //    dialog->show();

//    DialogTTFTest dialog(this);
//    dialog.setWindowFlags(dialog.windowFlags()|Qt::WindowMinMaxButtonsHint);
//    dialog.exec();

    DialogTTFTest *dialog=new DialogTTFTest(this);
    dialog->setAttribute(Qt::WA_DeleteOnClose,true);
    dialog->setWindowFlags(dialog->windowFlags()|Qt::WindowMinMaxButtonsHint);
    this->clearTestWidget();
    this->ui->widgetPlay->layout()->addWidget(dialog);

}

void MainWindow::onRegularShape_clicked()
{
    //RegularShapeTest dialog(this);
    //dialog.setWindowFlags(dialog.windowFlags()|Qt::WindowMinMaxButtonsHint);
    //dialog.exec();

    RegularShapeTest *dialog=new RegularShapeTest(this);
    dialog->setAttribute(Qt::WA_DeleteOnClose,true);
    dialog->setWindowFlags(dialog->windowFlags()|Qt::WindowMinMaxButtonsHint);
    this->clearTestWidget();
    this->ui->widgetPlay->layout()->addWidget(dialog);
}

void MainWindow::onSpeedoMeter_clicked()
{

    DialogSpeedometer *dialog=new DialogSpeedometer();
    dialog->setAttribute(Qt::WA_DeleteOnClose,true);
    dialog->setWindowFlags(dialog->windowFlags()|Qt::WindowMinMaxButtonsHint);
    //dialog->show();
    this->clearTestWidget();
    this->ui->widgetPlay->layout()->addWidget(dialog);
}

void MainWindow::onWaterWave_clicked()
{
    WaterWaveWidget *dialog =new WaterWaveWidget() ;
    dialog->setWindowTitle("水波模拟");
    dialog->setAttribute(Qt::WA_DeleteOnClose,true);
    dialog->setWindowFlags(dialog->windowFlags()|Qt::WindowMinMaxButtonsHint);
    //dialog->setWindowModality(Qt::WindowModal);
    //dialog->show();

    this->clearTestWidget();
    this->ui->widgetPlay->layout()->addWidget(dialog);
}
void MainWindow::clearTestWidget()
{
    QList<QWidget*> list=this->ui->widgetPlay->findChildren<QWidget*>(QString(), Qt::FindDirectChildrenOnly);
    for(QWidget * w:list)
    {
        delete  w;
    }
}
void MainWindow::testFun(int a)
{
    qDebug()<<a;
}
void MainWindow::on_action111_triggered()
{
    function<int(int)> fun1=[](int a){ return  a; };
    //function<void(int)> fun2=[](int a){qDebug()<<a;};
    //void (MainWindow::*fun2)(int);

    function<void(int)> fun2=bind(&MainWindow::testFun,this,std::_Placeholder<1>());
    //function<void(int)> fun2=bind(&MainWindow::testFun,this,std::placeholders::_1);
    worker.BeginInvoke2(fun1,fun2,100);

}
